package edu.ahpu.propertymanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import edu.ahpu.propertymanage.dataclass.PageSet;
import edu.ahpu.propertymanage.pojo.Message;
import edu.ahpu.propertymanage.service.MessageService;
import edu.ahpu.propertymanage.util.GetID;
import edu.ahpu.propertymanage.util.TimeChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    MessageService messageService;

    @Autowired
    TimeChange timeChange;

    @Autowired
    GetID getID;

    @PostMapping("messagepage")
    public IPage<Message> visitorPage(@RequestBody PageSet pageSet) {
        IPage<Message> messageIPage = new Page<>(pageSet.getCurrent(), pageSet.getSize());
        messageIPage = messageService.page(messageIPage);
        for (Message m : messageIPage.getRecords()) {
            m.setMsgBeginDate(timeChange.deleteTime(m.getMsgBeginDate()));
        }
        return messageIPage;
    }

    @GetMapping("addmessage")
    public String getMessageID() {
        return getID.createMessageID();
    }

    @PostMapping("addmessage")
    public String addMessage(@RequestBody Message message) {
        boolean bool = messageService.saveOrUpdate(message);
        if (bool) {
            return "success";
        } else
            return "error";
    }

    @PostMapping("deletemessgae")
    public String deleteMessage(String msgId) {
        System.out.println(msgId);
        boolean bool = messageService.removeById(msgId);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("detailmessage")
    public Message updateMessage(String msgId){
        System.out.println(msgId);
        Message message = messageService.getById(msgId);
        System.out.println(message);
        return message;
    }
}
